package com.elder.business.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.elder.business.domain.ElderService;
import com.elder.business.domain.ElderlyInfo;
import com.elder.business.domain.MedicationRecords;
import com.elder.business.domain.NurseContent;
import com.elder.business.mapper.ElderServiceMapper;
import com.elder.business.mapper.ElderlyInfoMapper;
import com.elder.business.mapper.NurseContentMapper;
import com.elder.business.service.IElderServiceService;
import com.elder.common.core.page.TableDataInfo;
import com.elder.common.utils.DateUtils;
import com.elder.common.utils.StringNewUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 老人购买服务Service业务层处理
 * 
 * @author elder
 * @date 2024-04-11
 */
@Service
public class ElderServiceServiceImpl implements IElderServiceService
{
    @Autowired
    private ElderServiceMapper elderServiceMapper;
    @Autowired
    private ElderlyInfoMapper elderlyInfoMapper;
    @Autowired
    private NurseContentMapper nurseContentMapper;

    /**
     * 查询老人购买服务
     * 
     * @param id 老人购买服务主键
     * @return 老人购买服务
     */
    @Override
    public JSONObject selectElderServiceById(Long id)
    {
        ElderService v = elderServiceMapper.selectElderServiceById(id);
        ElderlyInfo elder = Objects.isNull(v.getElderId())? new ElderlyInfo():elderlyInfoMapper.getElderInfoByElderId(v.getElderId());
        NurseContent nurse = Objects.isNull(v.getNurseId()) ? new NurseContent(): nurseContentMapper.selectNurseContentByNurseId(v.getNurseId());
        JSONObject obj = new JSONObject();
        obj.put("id",StringNewUtils.formatStr(v.getId()));
        obj.put("elderId",StringNewUtils.formatStr(v.getElderId()));
        obj.put("elderName",elder.getElderName());
        obj.put("nurseId",StringNewUtils.formatStr(v.getNurseId()));
        obj.put("serviceName",nurse.getNurseName());
        obj.put("duration",v.getDuration());
        obj.put("nursePrice",nurse.getNursePrice());
        obj.put("nurseDescribe",nurse.getNurseDescribe());
        obj.put("nurseNotes",nurse.getNotes());
        return obj;
    }

    /**
     * 查询老人购买服务列表
     * 
     * @param elderService 老人购买服务
     * @return 老人购买服务
     */
    @Override
    public List<ElderService> selectElderServiceList(ElderService elderService)
    {
        return elderServiceMapper.selectElderServiceList(elderService);
    }

    /**
     * 新增老人购买服务
     * 
     * @param elderService 老人购买服务
     * @return 结果
     */
    @Override
    public int insertElderService(ElderService elderService)
    {
        elderService.setCreateTime(DateUtils.getLongTime());
        return elderServiceMapper.insertElderService(elderService);
    }

    /**
     * 修改老人购买服务
     * 
     * @param elderService 老人购买服务
     * @return 结果
     */
    @Override
    public int updateElderService(ElderService elderService)
    {
        return elderServiceMapper.updateElderService(elderService);
    }

    /**
     * 批量删除老人购买服务
     * 
     * @param ids 需要删除的老人购买服务主键
     * @return 结果
     */
    @Override
    public int deleteElderServiceByIds(Long[] ids)
    {
        return elderServiceMapper.deleteElderServiceByIds(ids);
    }

    /**
     * 删除老人购买服务信息
     * 
     * @param id 老人购买服务主键
     * @return 结果
     */
    @Override
    public int deleteElderServiceById(Long id)
    {
        return elderServiceMapper.deleteElderServiceById(id);
    }

    @Override
    public TableDataInfo getElderServiceList(String elderName, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        List<Long> elderIds = null;
        if(!StringNewUtils.isEmpty(elderName)){
            List<ElderlyInfo> elderList = elderlyInfoMapper.getElderList(elderName);
            if(CollectionUtils.isNotEmpty(elderList)){
                elderIds = elderList.stream().map(ElderlyInfo::getElderId).collect(Collectors.toList());
            }
        }
        //用餐时间处理
        Long daystartTime = null;
        Long dayendTime = null;
        if(!StringNewUtils.isEmpty(startTime)&& !StringNewUtils.isEmpty(endTime)){
            Long currentTime = DateUtils.getTargetDateSeconds(startTime);
            Long endCurrentTime = DateUtils.getTargetDateSeconds(endTime);
            daystartTime = DateUtils.getDayStartTime(currentTime);
            dayendTime = DateUtils.getDayEndTime(endCurrentTime);
        }
        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<ElderService> list = elderServiceMapper.getElderServiceList(elderIds,daystartTime,dayendTime);
        PageInfo<ElderService> pageInfo = new PageInfo<>(list);
        List<ElderService> elderServiceList = pageInfo.getList();

        elderIds = elderServiceList.stream().map(ElderService::getElderId).collect(Collectors.toList());
        List<Long> serviceIds = elderServiceList.stream().map(ElderService::getNurseId).collect(Collectors.toList());
        Map<Long,String> serviceMap = CollectionUtils.isEmpty(serviceIds)?new HashMap<>():
                nurseContentMapper.getNurseContentList(serviceIds)
                    .stream().collect(Collectors.toMap(NurseContent::getNurseId,NurseContent::getNurseName));
        //获取住户信息
        Map<Long,String> elderMap = CollectionUtils.isEmpty(elderIds)?new HashMap<>():
                elderlyInfoMapper.getElderInfoByElderIds(elderIds)
                        .stream().collect(Collectors.toMap(ElderlyInfo::getElderId,ElderlyInfo::getElderName));
        List<JSONObject> returnList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(elderServiceList)){
            returnList = elderServiceList.stream().map(v->{
                JSONObject obj = new JSONObject();
                obj.put("id",StringNewUtils.formatStr(v.getId()));
                obj.put("elderId",StringNewUtils.formatStr(v.getElderId()));
                obj.put("elderName",elderMap.get(v.getElderId()));
                obj.put("nurseId",StringNewUtils.formatStr(v.getNurseId()));
                obj.put("isFinish",v.getIsFinish());
                obj.put("serviceName",serviceMap.get(v.getNurseId()));
                obj.put("duration",v.getDuration());
                return obj;
            }).collect(Collectors.toList());
        }
        TableDataInfo date = new TableDataInfo();
        date.setCode(200);
        date.setTotal(pageInfo.getTotal());
        date.setRows(returnList);
        return date;
    }

    @Override
    public Integer updateElderServiceisFinish(Long serviceId, String status) {
        if(Objects.equals("YES",status)){
            return elderServiceMapper.updateElderServiceFinish(serviceId,1);
        }
        return 1;
    }
}
